//
//  PGTitleView.m
//  EpointFrame5
//
//  Created by Pill Gong on 14-4-16.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "PGTitleView.h"
#import "UIView+Addtions.h"

#define BlackColor [UIColor blackColor]
#define WhiteColor [UIColor whiteColor]

#define TitleViewWidth 150
#define TitleViewHeight 30

@interface PGTitleView ()

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UILabel *titleLable;
@property (nonatomic, strong) UIImageView *arrowImage;

@end

@implementation PGTitleView

- (id)initWithFrame:(CGRect)frame
{
    
    self = [super initWithFrame:CGRectMake(0, 0, TitleViewWidth, TitleViewHeight)];
    if (self) {
        [self addSubviews];
        [self setDefaultValues];
    }
    return self;
}

- (void)addSubviews {
    
    self.contentView = [[UIView alloc] initWithFrame:self.bounds];
    [self addSubview:_contentView];

    self.titleLable = [[UILabel alloc] init];
    [_contentView addSubview:_titleLable];
    
    self.arrowImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 8, 5)];
    _arrowImage.image = [UIImage imageNamed:@"gold_pull"];//gold_pull //expandableImage
    [_contentView addSubview:_arrowImage];
}

- (void)setDefaultValues {
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentViewTap:)];
    [_contentView addGestureRecognizer:tap];
    
    _titleLable.font = [UIFont boldSystemFontOfSize:17.0];
    _titleLable.textAlignment = NSTextAlignmentCenter;
    _titleLable.backgroundColor = [UIColor clearColor];
    _titleLable.textColor = WhiteColor;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [_titleLable sizeToFit];
    _titleLable.width += 10;
    
    _titleLable.centerY = _arrowImage.centerY = _contentView.height*0.5;
    _titleLable.centerX = _contentView.width*0.5-5;
    
    _arrowImage.left = _titleLable.right+5;
}

#pragma mark - property

- (void)setText:(NSString *)text {
    _text = text;
    _titleLable.text = _text;
}

- (void)setFont:(UIFont *)font {
    _font = font;
    _titleLable.font = _font;
}

- (void)setIsSelected:(BOOL)isSelected {
    if (_isSelected == isSelected) {
        return;
    }
    _isSelected = isSelected;
    [self rotateArrowImage];
}

- (void)setTitleColor {
    
}

#pragma mark - gesture and animation

- (void)contentViewTap:(UITapGestureRecognizer *)tap {
    if (tap.state == UIGestureRecognizerStateRecognized) {
        if (_delegate && [_delegate respondsToSelector:@selector(titleViewDidSelected:)]) {
            [_delegate titleViewDidSelected:self];
        }
        self.isSelected = !_isSelected;
    }
}

- (void)rotateArrowImage {
    [UIView animateWithDuration:.25 animations:^{
        //code
        if (_isSelected) {
            _arrowImage.transform = CGAffineTransformMakeRotation(M_PI);
        } else {
            _arrowImage.transform = CGAffineTransformMakeRotation(0);
        }
    } completion:^(BOOL finished) {
        //finish
    }];
}

@end
